home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AppsToGo / DTS.Draw / ToolPalette.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-22  |  7.0 KB  |  275 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        ToolPalette.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the DTS.Draw
  20. ** tool palette document.  The tool palette document does very little, so most of
  21. ** the document procedure pointers are set to nil.  Imaging and clicking are the
  22. ** only two methods that we need to support. */
  23.  
  24.  
  25.  
  26. /*****************************************************************************/
  27.  
  28.  
  29.  
  30. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  31. #include "App.defs.h"        /* Get various application definitions.            */
  32. #include "App.protos.h"        /* Get the prototypes for the application.        */
  33.  
  34. #ifndef __ERRORS__
  35. #include <Errors.h>
  36. #endif
  37.  
  38. #ifndef __FONTS__
  39. #include <Fonts.h>
  40. #endif
  41.  
  42. #ifndef __RESOURCES__
  43. #include <Resources.h>
  44. #endif
  45.  
  46. #ifndef __TOOLUTILS__
  47. #include <ToolUtils.h>
  48. #endif
  49.  
  50. #ifndef __TREEOBJ2__
  51. #include "TreeObj2.h"
  52. #endif
  53.  
  54. #ifndef __UTILITIES__
  55. #include "Utilities.h"
  56. #endif
  57.  
  58.  
  59. /*****************************************************************************/
  60.  
  61.  
  62.  
  63. static void        ToolContentClick(WindowPtr window, EventRecord *event, Boolean firstClick);
  64. static OSErr    ToolImageDocument(FileRecHndl frHndl);
  65. static Rect        PlaceToolWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo);
  66.  
  67.  
  68.  
  69. /*****************************************************************************/
  70. /*****************************************************************************/
  71.  
  72. #ifdef applec
  73. #pragma segment DTSDrawSeg2
  74. #endif
  75.  
  76. /*****************************************************************************/
  77. /*****************************************************************************/
  78.  
  79.  
  80.  
  81. /* Just imaging and clicking are handles by the tool palette. */
  82.  
  83. OSErr    ToolInitDocument(FileRecHndl frHndl)
  84. {
  85.     FileRecPtr    frPtr;
  86.  
  87.     frPtr = *frHndl;
  88.     frPtr->fileState.calcFrameRgnProc        = nil;
  89.     frPtr->fileState.contentClickProc        = ToolContentClick;
  90.     frPtr->fileState.contentKeyProc          = nil;
  91.     frPtr->fileState.drawFrameProc           = nil;
  92.     frPtr->fileState.freeDocumentProc        = nil;
  93.     frPtr->fileState.freeWindowProc          = nil;
  94.     frPtr->fileState.imageProc               = ToolImageDocument;
  95.     frPtr->fileState.readDocumentProc        = nil;
  96.     frPtr->fileState.readDocumentHeaderProc  = nil;
  97.     frPtr->fileState.resizeContentProc       = nil;
  98.     frPtr->fileState.scrollFrameProc         = nil;
  99.     frPtr->fileState.undoFixupProc           = nil;
  100.     frPtr->fileState.windowCursorProc        = nil;
  101.     frPtr->fileState.writeDocumentProc       = nil;
  102.     frPtr->fileState.writeDocumentHeaderProc = nil;
  103.  
  104.     return(noErr);
  105. }
  106.  
  107.  
  108.  
  109. /*****************************************************************************/
  110. /*****************************************************************************/
  111.  
  112.  
  113.  
  114. /* Find out which tool was clicked or double-clicked on. */
  115.  
  116. static void    ToolContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  117. {
  118. #ifndef __MWERKS__
  119. #pragma unused (firstClick)
  120. #endif
  121.  
  122.     short            cnum;
  123.     ControlHandle    ctl;
  124.     MenuHandle        menu;
  125.     short            i;
  126.  
  127.     cnum = IsCtlEvent(window, event, &ctl, nil);
  128.     if (cnum) {
  129.         menu = GetMenuHandle(mToolPalette);
  130.         if (menu) {
  131.             for (i = kArrowTool; i <= kNumTools; ++i) SetItemMark(menu, i, noMark);
  132.             SetItemMark(menu, UnmapMItem(mToolPalette, cnum - rArrowIcon + 1),
  133.                         '0' + (*ctl)->contrlValue);
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. /*****************************************************************************/
  141.  
  142.  
  143.  
  144. /* Draw the tool palette icons into the port.  If DTS.LIB..framework calls us, the
  145. ** port is already set, but we set it here so that we can call this function
  146. ** directly.  Calling it directly is desireable for certain operations, such
  147. ** as when the user clicks with a one-shot tool.  When this occurs, we revert
  148. ** back to the arrow tool.  Also, if a tool is permanently selected and the
  149. ** user clicks in a document but then doesn't grow out an object, we also revert
  150. ** to the arrow tool.  Another case is if the user selects a tool via the menu. */
  151.  
  152. static OSErr    ToolImageDocument(FileRecHndl frHndl)
  153. {
  154. #ifndef __MWERKS__
  155. #pragma unused (frHndl)
  156. #endif
  157.  
  158.     DoDrawControls((*frHndl)->fileState.window, false);
  159.     return(noErr);
  160. }
  161.  
  162.  
  163.  
  164. /*****************************************************************************/
  165. /*****************************************************************************/
  166.  
  167.  
  168.  
  169. Rect    PlaceToolWindow(WindowPtr window, WindowPtr relatedWindow, Rect sizeInfo)
  170. {
  171. #ifndef __MWERKS__
  172. #pragma unused (relatedWindow, sizeInfo)
  173. #endif
  174.  
  175.     Rect    scnRct, cntRct;
  176.     short    dx, dy;
  177.  
  178.     scnRct = GetMainScreenRect();
  179.     cntRct = GetWindowContentRect(window);
  180.  
  181.     dx = cntRct.right  - cntRct.left;
  182.     dy = cntRct.bottom - cntRct.top;
  183.  
  184.     cntRct.right  = scnRct.right - 5;
  185.     cntRct.top    = scnRct.top   + 33;
  186.     cntRct.left   = cntRct.right - dx;
  187.     cntRct.bottom = cntRct.top + dy;
  188.  
  189.     MoveWindow(window, cntRct.left, cntRct.top, false);
  190.     return(cntRct);
  191. }
  192.  
  193.  
  194.  
  195. /*****************************************************************************/
  196.  
  197.  
  198.  
  199. /* Tool palette set tool function. */
  200.  
  201. void    SetPaletteTool(short tool)
  202. {
  203.     MenuHandle        menu;
  204.     WindowPtr        toolWind;
  205.     ControlHandle    ctl;
  206.     short            i;
  207.  
  208.     toolWind = GetNextWindow(nil, kToolFileType);
  209.     if (toolWind) BeginContent(toolWind);
  210.     else {
  211.         toolWind = GetNextWindow(nil, kDocFileType);
  212.         if (toolWind) BeginFrame(toolWind);
  213.     }
  214.     if (!toolWind) return;
  215.  
  216.     menu = GetMenuHandle(mToolPalette);
  217.     for (ctl = nil, i = 1; (ctl = CCIconNext(toolWind, ctl, 1, true)) != nil; ++i) {
  218.         if (Ctl2CNum(ctl) - rArrowIcon == tool) {
  219.             SetControlValue(ctl, 1);
  220.             if (menu) SetItemMark(menu, i, '1');
  221.         }
  222.         else {
  223.             SetControlValue(ctl, 0);
  224.             if (menu) SetItemMark(menu, i, noMark);
  225.         }
  226.     }
  227.  
  228.     EndContent(toolWind);        /* Or EndFrame -- interchangeable calls. */
  229. }
  230.  
  231.  
  232.  
  233. /*****************************************************************************/
  234.  
  235.  
  236.  
  237. short    GetTool(void)
  238. {
  239.     WindowPtr        toolWind;
  240.     ControlHandle    ctl;
  241.  
  242.                    toolWind = GetNextWindow(nil, kToolFileType);
  243.     if (!toolWind) toolWind = GetNextWindow(nil, kDocFileType);
  244.     if (!toolWind) return(0);
  245.  
  246.     for (ctl = nil; (ctl = CCIconNext(toolWind, ctl, 1, true)) != nil;)
  247.         if ((*ctl)->contrlValue) break;
  248.  
  249.     return(Ctl2CNum(ctl) - rArrowIcon);
  250. }
  251.  
  252.  
  253.  
  254. /*****************************************************************************/
  255.  
  256.  
  257.  
  258. Boolean    GetToolPersistence(void)
  259. {
  260.     WindowPtr        toolWind;
  261.     ControlHandle    ctl;
  262.  
  263.                    toolWind = GetNextWindow(nil, kToolFileType);
  264.     if (!toolWind) toolWind = GetNextWindow(nil, kDocFileType);
  265.     if (!toolWind) return(0);
  266.  
  267.     for (ctl = nil; (ctl = CCIconNext(toolWind, ctl, 1, true)) != nil;)
  268.         if ((*ctl)->contrlValue == 2) return(true);
  269.  
  270.     return(false);
  271. }
  272.  
  273.  
  274.  
  275.